﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace TreeViewList.UC
{
    public partial class UCTreeViewList : System.Web.UI.UserControl
    {
        public class NodeItem
        {
            public int ID { get; set; }
            public string Title { get; set; }
            public int Level { get; set; }
            public string ChildPath { get; set; }
        }      

        public List<NodeItem> NodeItems { get; set; }

        /// <summary>
        /// 绑定TreeViewList控件   
        /// </summary>
        public void Bind()
        {
            this.rptListView.DataSource = NodeItems ?? new List<NodeItem>();
            this.rptListView.DataBind();
        }

        protected void rptListView_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                var LitFirst = (Literal)e.Item.FindControl("LitFirst");
                var LabClassNm = (Label)e.Item.FindControl("LabClassNm");

                string LitStyle = "<span style='margin-left:{0}px;'>{1}{2}</span>";
                string LitImg1 = "<img src='/hgmanage/public/uc/treeview/img/tree_folder3.gif'>";
                string LitImg2 = "<img src='/hgmanage/public/uc/treeview/img/tree_folder3.gif'>";
                string LitImg3 = "<img src='/hgmanage/public/uc/treeview/img/tree_line1.gif'>";

                NodeItem drv = (NodeItem)e.Item.DataItem;
                var vLevel = drv.Level;

                if (vLevel == 1)
                {
                    LabClassNm.Font.Bold = true;
                    LitFirst.Text = LitImg1;
                }
                else
                {
                    if (!IsTitleBold(drv.ID, drv.ChildPath)) 
                    {
                        LabClassNm.Font.Bold = true;
                    }
                    LitFirst.Text += string.Format(LitStyle, (vLevel - 1) * 20, LitImg3, LitImg2);
                }
            }
        }

        private bool IsTitleBold(int id,string childpath) 
        {
            return id.ToString().Equals(childpath);
        }
     
    }
}